package com.station.Activitys.http.manager;

import android.app.Activity;
import android.util.Log;

import com.station.Activitys.bean.AppUpdate;
import com.station.Activitys.bean.ImageText;
import com.station.Activitys.bean.LatestNews;
import com.station.Activitys.bean.Linkage;
import com.station.Activitys.bean.Spinner;
import com.station.Activitys.http.ApiService;
import com.station.Activitys.http.NetWorkMgr;
import com.station.Activitys.http.api.HomeAPI;
import com.station.Activitys.utils.Constant;

import org.simple.eventbus.EventBus;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 项目名称：
 * 类描述：首页 接口管理 manager
 * 创建人：张飞祥
 * 创建时间：2018/5/9 0027 下午 1:33
 * 修改人：张飞祥
 * 修改时间：2018/5/9 0027 下午 1:33
 * 修改备注：
 */
public class HomeManager {

    private static HomeAPI mHomeAPI;
    private static ApiService mApiService;
    private static Activity mActivity;
    private static int mTaskId;

    //加载数据类型
    private int RefType = Constant.FIRST_LOAD;

    private static class HomeApiManagerHolder {
        private static final HomeManager INSTANCE = new HomeManager();
    }
    public static final HomeManager getHomeApiManager(Activity activity, int taskId) {
        mHomeAPI = NetWorkMgr.getInstance(activity)
                .getServiceAPI(HomeAPI.class);
        mApiService = ApiService.getApiService();
        mActivity = activity;
        mTaskId = taskId;
        return HomeApiManagerHolder.INSTANCE;
    }

    public void getType(int type){
        RefType=type;
    }

    /**
     * APP 检查更新接口
     */
    public void inspectUpdateApp(String appType) {
        Subscription subscription = mHomeAPI
                .inspectUpdateApp(appType).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AppUpdate>() {
                    @Override
                    public void onCompleted() {

                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                        //log 打印
                        EventBus.getDefault().post(e.toString(), Constant.BUS_APP_UPDATE_ERROR);
                    }
                    @Override
                    public void onNext(AppUpdate bean) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(bean, Constant.BUS_APP_UPDATE);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }



    /**
     * 获取 所有 下拉集合 列表
     */
    public void appAllSelect() {
        Subscription subscription = mHomeAPI
                .appAllSelect().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Spinner>>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_APP_ALL_SELECT_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<Spinner> list) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(list, Constant.BUS_APP_ALL_SELECT);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 获取 三级联动数据 列表
     */
    public void appGetLinkage() {
        Subscription subscription = mHomeAPI
                .appGetLinkage().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Linkage>>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_APP_GET_LINKAGE_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<Linkage> list) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(list, Constant.BUS_APP_GET_LINKAGE);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 轮播图
     */
    public void homeBanner() {
        Subscription subscription = mHomeAPI
                .homeBanner().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ImageText>>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_BANNER_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(List<ImageText> list) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(list, Constant.BUS_HOME_BANNER);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }

    /**
     * 最新消息
     */
    public void homeLatestNews(int userId) {
        Subscription subscription = mHomeAPI
                .homeLatestNews(userId).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LatestNews>() {
                    @Override
                    public void onCompleted() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(e.toString(), Constant.BUS_HOME_LATEST_NEWS_ERROR);
                        //log 打印
                        Log.e(mActivity.getClass().getSimpleName(),"structureList 错误" + e.toString());
                    }
                    @Override
                    public void onNext(LatestNews bean) {
                        //返回成功 数据处理
                        EventBus.getDefault().post(bean, Constant.BUS_HOME_LATEST_NEWS);
                    }
                });
        mApiService.getCompositeSubscription(mTaskId).add(subscription);
    }
}
